package com.jsgt.greekfire.ui.entrust

import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import com.fhx.core.base.BaseFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.jsgt.greekfire.R
import com.jsgt.greekfire.ui.entrust.fragment.EntrustNodeFragment
import com.jsgt.greekfire.ui.entrust.fragment.MyEntrustFragment
import com.jsgt.greekfire.ui.mainassets.adapter.TabEntity
import com.jsgt.greekfire.ui.trading.adapter.PagerAdapter
import kotlinx.android.synthetic.main.fragment_entrust.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class EntrustFragment : BaseFragment() {
    private var param1: String? = null
    private var param2: String? = null
    private var mTabEntities =
        ArrayList<CustomTabEntity>()
    private val mFragments: MutableList<BaseFragment> = ArrayList()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            EntrustFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    override fun getLayoutId(): Int = R.layout.fragment_entrust

    override fun main() {
        val tabArray =
            resources.getStringArray(R.array.tab_coin_entrust_list)
        val tabs = java.util.ArrayList<String>()

        mFragments.add(EntrustNodeFragment()) //委托节点
        mFragments.add(MyEntrustFragment()) //我的委托
        for (i in tabArray.indices) {
            mTabEntities.add(TabEntity(tabArray!![i]))
            tabs.add(tabArray[i])
        }
        viewPagerEntrust.adapter = PagerAdapter(
            childFragmentManager,
            mFragments,
            tabs
        )
        tl_2()
    }

    private fun tl_2() {
        commonTablEntrust.setTabData(mTabEntities)
        commonTablEntrust.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                viewPagerEntrust.setCurrentItem(position)
            }

            override fun onTabReselect(position: Int) {}
        })
        viewPagerEntrust.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                commonTablEntrust.setCurrentTab(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        viewPagerEntrust.setCurrentItem(0)
    }

}